package main

import (
	"fmt"
	"time"

	"gitee.com/wu-jin-feng/fgin/demos/fopentelemetry_test/fotel"
	"github.com/gin-gonic/gin"
	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/trace"
)

var (
	tracer trace.Tracer
)

func doneFunc1(c *gin.Context) {
	// tracer := otel.Tracer(c.Request.URL.Path)
	spanCtx, span := tracer.Start(c.Request.Context(), "doneFunc1", trace.WithTimestamp(time.Now().UTC()))
	defer span.End()
	c.Request = c.Request.WithContext(spanCtx)
	span.AddEvent("doneFunc1", trace.WithAttributes(
		attribute.String("abs", "test"),
	))
	time.Sleep(200 * time.Millisecond)
}

func doneFunc2(c *gin.Context) {
	// tracer := otel.Tracer(c.Request.URL.Path)
	_, span := tracer.Start(c.Request.Context(), "doneFunc2", trace.WithTimestamp(time.Now().UTC()))
	defer span.End()
	// jaeger日志部分
	span.AddEvent("doneFunc2", trace.WithAttributes(
		attribute.String("abs", "test"),
	))
	time.Sleep(200 * time.Millisecond)
}

func mainV1() {
	fotel.InitTracer("localhost:4318", "mainV1")
	r := gin.Default()

	r.GET("/hello", func(c *gin.Context) {
		// 手动创建Span
		tracer = otel.Tracer(c.Request.URL.Path)
		spanCtx, span := tracer.Start(c.Request.Context(), "/hello", trace.WithTimestamp(time.Now().UTC()))
		c.Request = c.Request.WithContext(spanCtx)
		defer span.End()
		doneFunc1(c)
		doneFunc2(c)
		// jaeger tag部分
		span.SetAttributes(
			attribute.String("测试上报", "test"),
			attribute.String("测试上报1", "test1"),
			attribute.String("测试上报2", "test2"),
		)
		// span.SetStatus(codes.Error, "request error")
		// span.RecordError(errors.New("请求错误"))
		c.JSON(200, gin.H{"message": "Hello OTel!"})
	})
	r.GET("/hello2", func(c *gin.Context) {
		// 手动创建Span
		tracer = otel.Tracer(c.Request.URL.Path)
		spanCtx, span := tracer.Start(c.Request.Context(), "/hello", trace.WithTimestamp(time.Now().UTC()))
		c.Request = c.Request.WithContext(spanCtx)
		defer span.End()
		doneFunc1(c)
		doneFunc2(c)
		// jaeger tag部分
		span.SetAttributes(
			attribute.String("测试上报", "test"),
			attribute.String("测试上报1", "test1"),
			attribute.String("测试上报2", "test2"),
		)
		// span.SetStatus(codes.Error, "request error")
		// span.RecordError(errors.New("请求错误"))
		c.JSON(200, gin.H{"message": "Hello OTel!"})
	})
	r.Run("127.0.0.1:8080")
}

func doneFuncV1(c *gin.Context, tracer *fotel.Tracer) {
	fmt.Println("doneFunc1 开始时间:", time.Now().UnixMilli())
	spanCtx, span := tracer.Start(c, "doneFunc1")
	defer func() {
		fmt.Println("doneFunc1 结束时间:", time.Now().UnixMilli())
		tracer.End(span, nil)
	}()
	c.Request = c.Request.WithContext(spanCtx)
	span.AddEvent("doneFunc1", trace.WithAttributes(
		attribute.String("abs", "test"),
	))
	time.Sleep(200 * time.Millisecond)
}

func doneFuncV2(c *gin.Context, tracer *fotel.Tracer) {
	fmt.Println("doneFunc2 开始时间:", time.Now().UnixMilli())
	spanCtx, span := tracer.Start(c.Request.Context(), "doneFunc2")
	defer func() {
		fmt.Println("doneFunc2 结束时间:", time.Now().UnixMilli())
		tracer.End(span, nil)
	}()
	c.Request = c.Request.WithContext(spanCtx)
	span.AddEvent("doneFunc1", trace.WithAttributes(
		attribute.String("abs", "test"),
	))
	time.Sleep(200 * time.Millisecond)
}

func main() {
	// time.Local = time.UTC
	fotel.InitTracer("localhost:4318", "otel_demo")
	r := gin.Default()

	r.GET("/hello", func(c *gin.Context) {
		// 手动创建Span
		tracer := fotel.NewTracer(trace.SpanKindClient, "otel_demo1")
		fmt.Println("hello 开始时间:", time.Now().UnixMilli())
		spanCtx, span := tracer.Start(c.Request.Context(), "/hello1")
		c.Request = c.Request.WithContext(spanCtx)
		header := fotel.HttpInject(spanCtx, c.Request.Header)
		fmt.Println("111", header)
		for k, v := range header {
			for _, hv := range v {
				c.Request.Header.Add(k, hv)
			}
		}

		defer func() {
			fmt.Println("hello 结束时间:", time.Now().UnixMilli())
			tracer.End(span, nil)
		}()
		doneFuncV1(c, tracer)
		doneFuncV2(c, tracer)
		// jaeger tag部分
		span.SetAttributes(
			attribute.String("测试上报", "test"),
			attribute.String("测试上报1", "test1"),
			attribute.String("测试上报2", "test2"),
		)
		// span.SetStatus(codes.Error, "request error")
		// span.RecordError(errors.New("请求错误"))
		c.JSON(200, gin.H{"message": "Hello OTel!"})
	})
	r.GET("/hello2", func(c *gin.Context) {
		// 手动创建Span
		tracer = otel.Tracer(c.Request.URL.Path)
		spanCtx, span := tracer.Start(c.Request.Context(), "/hello", trace.WithTimestamp(time.Now().UTC()))
		c.Request = c.Request.WithContext(spanCtx)
		defer span.End()
		doneFunc1(c)
		doneFunc2(c)
		// jaeger tag部分
		span.SetAttributes(
			attribute.String("测试上报", "test"),
			attribute.String("测试上报1", "test1"),
			attribute.String("测试上报2", "test2"),
		)
		// span.SetStatus(codes.Error, "request error")
		// span.RecordError(errors.New("请求错误"))
		c.JSON(200, gin.H{"message": "Hello OTel!"})
	})
	r.Run("127.0.0.1:8080")
}
